package marksys.free.com.marksys.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 项目名称 wleducation
 * 创建人 梧桐树
 * 创建时间  2018/8/20 14:35
 * 功能描述：日期帮助类
 */

public class DateUtils {

    /**
     * 基姆拉尔森计算公式根据日期判断星期几
     */
    public static void CalculateWeekDay(int y, int m, int d) {
        if (m == 1 || m == 2) {
            m += 12;
            y--;
        }
        int iWeek = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
        switch (iWeek) {
            case 0:
                System.out.printf("周一\n");
                break;
            case 1:
                System.out.printf("周二\n");
                break;
            case 2:
                System.out.printf("周三\n");
                break;
            case 3:
                System.out.printf("周四\n");
                break;
            case 4:
                System.out.printf("周五\n");
                break;
            case 5:
                System.out.printf("周六\n");
                break;
            case 6:
                System.out.printf("周日\n");
                break;
            default:
                break;
        }
    }

    public static String dateToWeek(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
        return dateStrToWeek(simpleDateFormat.format(date));
    }

    /**
     * 日期转星期
     *
     * @param datetime
     * @return
     */
    public static String dateStrToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 获取指定格式的当前时间字符串
     *
     * @param format
     * @return
     */
    public static String getDataStrForFormat(String format) {
        String str = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
        str = sdf.format(new Date());
        return str;
    }

    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }
}
